/**
 * 弹出层对象
 */
MPT.addObject('Dialog', function() {
    var self = this;
    var element_jQ;
    var box_jQ;
    var close_jQ;
    var child_name;
    var options;
    var children_html = {};
    var child_elm;
    var is_show = false;

    // 显示现有内容层
    this.show = function(_child_name, _options) {
        Debug.info('Dialog.show');

        child_name = _child_name;
        options = _options;
        options['show_ok_fun'] = _options['success'] || function() { };

        appendChild();

        setCss();
        setDialogSize();
        setBoxPosition();

        is_show = true;
    };

    this.hide = function() {
        element_jQ.hide();

        is_show = false;

        if (child_elm) jQuery(child_elm).remove();
    };


    // 设置对话框（对话框包含覆盖层）大小
    var setDialogSize = function() {
        element_jQ.hide()
		.css({
		    "width": document.body.scrollWidth,
		    "height": (document.body.scrollHeight > document.documentElement.clientHeight ? document.body.scrollHeight : document.documentElement.clientHeight)
		})
		.show();
    };

    // 设置内容层位置
    var setBoxPosition = function() {
        var left = (jQuery(window).width() - box_jQ.width()) / 2;
        var top = (jQuery(window).height() - box_jQ.height()) / 2 + jQuery(window).scrollTop();
        top = (top > 0 ? top : 0);
        box_jQ.css({
            "left": left,
            "top": top,
            "opacity": 1
        });
    };

    //设置内容层的高度、宽度、透明度
    var setCss = function() {
        element_jQ.show();

        box_jQ.css({
            "width": options['width'] || 100,
            "height": options['height'] || 100,
            "opacity": 0
        });
    };

    var appendChild = function() {
        loadChildHtml(function() {

            element_jQ = $('.a_websgs_dialog');
            box_jQ = $('.mpt_dialog_box', element_jQ);
            box_jQ.html(child_elm);            
            
            close_jQ = $(".mpt_dialog_close", element_jQ);
            close_jQ.bind('click', function() { self.hide(); });

            options['show_ok_fun'](child_elm);

            Debug.info(child_elm);

        });
    };

    var loadChildHtml = function(_load_ok_fun) {
        if (!children_html[child_name] || options['cache'] == false) {  //重新载入新的弹出框
            children_html[child_name] = options['child_html'];
            child_elm = jQuery(children_html[child_name]).get(0);
            _load_ok_fun();

        }
        else {  //启用缓存中的弹出框
            child_elm = jQuery(children_html[child_name]).get(0);
            _load_ok_fun();
        }
    };

    //滚动条滚动时的事件
    $(window).bind("resize", function() {
        if (!is_show) return;
        setDialogSize();
        setBoxPosition();
    })
	.bind("scroll", function() {
	    if (!is_show) return;
	    var top = (jQuery(window).height() - box_jQ.height()) / 2 + jQuery(window).scrollTop();
	    top = (top > 0 ? top : 0);
	    box_jQ.css("top", top);
	});

});
